package com.xqkad.myfile.tools;

import android.util.Log;

public class MyFormat {
	public static final String
			IMAGE	="IMAGE",
			AUDIO	="AUDIO",
			VIDEO	="VIDEO",
			APK		="APK",
			TEXT	="TEXT",
			WEBTEXT	="WEBTEXT",
			WORD	="WORD",
			EXCEL	="EXCEL",
			PDF		="PDF",
			RAR		="RAR",
			PPT		="PPT",
			OTHER	="OTHER";
	public static final String[]
		Images 		= {".png",".jpg",".jpeg",".gif",".bmp"},
		Audios 		= {".aac",".ape",".flac",".mp3",".ogg",".m4a",".midi",".wav",".wma"},
		Videos 		= {".mp4",".flv",".3gp",".mkv",".rmvb",".avi"},
		Packages 	= {".apk"},
		Texts 		= {".txt"},
		WebTexts 	= {".htm",".html",".js",".jsp",".xml"},
		Words 		= {".doc",".docx"},
		Excels 		= {".xls",".xlsx"},
		Pdfs 		= {".pdf"},
		Rars 		= {".rar",".zip",".gz"},
		PPTs 		= {".ppt",".pptx"};
	
	
	public static boolean checkFormat(String filename, String[] formats) {
		String lowname = toLowStr(filename);

		for (String fmt : formats) {
			if (lowname.endsWith(toLowStr(fmt)))
				return true;
		}
		return false;
	}

	public static String getFormat(String filename){
		if (MyFormat.checkFormat(filename, MyFormat.Images)) {
			return IMAGE;
		} else if (MyFormat.checkFormat(filename, MyFormat.Audios)) {
			return AUDIO;
		} else if (MyFormat.checkFormat(filename, MyFormat.Videos)) {
			return VIDEO;
		} else if (MyFormat.checkFormat(filename, MyFormat.Packages)) {
			return APK;
		} else if (MyFormat.checkFormat(filename, MyFormat.Texts)) {
			return TEXT;
		} else if (MyFormat.checkFormat(filename, MyFormat.WebTexts)) {
			return WEBTEXT;
		} else if (MyFormat.checkFormat(filename, MyFormat.Pdfs)) {
			return PDF;
		} else if (MyFormat.checkFormat(filename, MyFormat.Words)) {
			return WORD;
		} else if (MyFormat.checkFormat(filename, MyFormat.Excels)) {
			return EXCEL;
		} else if (MyFormat.checkFormat(filename, MyFormat.PPTs)) {
			return PPT;
		} else if(MyFormat.checkFormat(filename, MyFormat.Rars)){
			return RAR;
		}else
			return OTHER;
	}

	private static String toLowStr(String s){
		if(s == null)
			return null;
		StringBuffer sb = new StringBuffer();

		for(int i=0;i<s.length();i++){
			char c = s.charAt(i);
			if(Character.isUpperCase(c))
				c = Character.toLowerCase(c);
			sb.append(c);
		}
		return sb.toString();
	}
}
